package com.wlanauth.dao.common.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wlanauth.dao.common.CommonDao;

public abstract class CommonDaoImpl<T extends Serializable> extends HibernateDaoSupport implements CommonDao<T> {
	
	@SuppressWarnings("unchecked")
	Class<T> entityClass = (Class<T>)((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]; 
	
	public void save(T t) throws DataAccessException {
		this.getHibernateTemplate().save(t);
	}

	public void update(T t) throws DataAccessException {
		this.getHibernateTemplate().update(t) ;
	}

	public void deleteByKey(Serializable key) throws DataAccessException{
		this.delete(this.load(key));
	}

	public void delete(T t) throws DataAccessException{
		this.getHibernateTemplate().delete(t);
	}

	@SuppressWarnings("unchecked")
	public T load(Serializable key) throws DataAccessException{
		return (T) this.getHibernateTemplate().load(entityClass, key);
	}

	@SuppressWarnings("unchecked")
	public T get(Serializable key) throws DataAccessException{
		
		return (T) this.getHibernateTemplate().get(entityClass, key);
	}	
	
	public void deleteAll(Collection<?> entities) throws DataAccessException{
		
		this.getHibernateTemplate().deleteAll(entities);
	}
	
}
